<?php

namespace App\Admin\Controllers\User;

use App\Admin\Actions\User\WalletRecord;
use App\Models\User\SystemUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SystemUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = ' ';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SystemUser());
        $grid->model()->orderBy('created_at','desc');
        $grid->actions(function ($actions) {
//            $actions->add(new WalletRecord());
        });

//        $grid->column('id', __('SystemUser.id'));
        $grid->column('no', __('SystemUser.no'));
        $grid->column('nickname', __('SystemUser.nickname'));
//        $grid->column('username', __('SystemUser.username'));
        $grid->column('phone', __('SystemUser.phone'));
        $grid->column('email', __('SystemUser.email'));
//        $grid->column('parent_id', __('SystemUser.parent_id'))->parentinfo();
//        $grid->column('wallet', __('SystemUser.wallet'));
//        $grid->column('integral', __('SystemUser.integral'));
//        $grid->column('openid', __('SystemUser.openid'));
        $grid->column('avatar_url', __('SystemUser.avatar_url'))->image('',50);
//        $grid->column('province', __('SystemUser.province'));
//        $grid->column('city', __('SystemUser.city'));
        $grid->column('gender', __('SystemUser.gender'))->gender();
//        $grid->column('remark', __('SystemUser.remark'))->modalinfo();
//        $grid->column('expire_at', __('SystemUser.expire_at'));
        $grid->column('last_login', __('SystemUser.last_login'));
//        $grid->column('longitude', __('SystemUser.longitude'));
//        $grid->column('latitude', __('SystemUser.latitude'));
//        $grid->column('level', __('SystemUser.level'));
//        $grid->column('distribution_level', __('SystemUser.distribution_level'));
//        $grid->column('remember_token', __('SystemUser.remember_token'));
        $grid->column('created_at', __('SystemUser.created_at'));
//        $grid->column('updated_at', __('SystemUser.updated_at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SystemUser::findOrFail($id));
//        dd($show->getModel()->username);
//        $show->column('test_name')->unserialize($show->getModel()->id);
        $show->field('id', __('SystemUser.id'));
        $show->field('nickname', __('SystemUser.nickname'));
        $show->field('username', __('SystemUser.username'));
        $show->field('phone', __('SystemUser.phone'));
        $show->field('email', __('SystemUser.email'));
        $show->field('password', __('SystemUser.password'));
        $show->field('parent_id', __('SystemUser.parent_id'));
        $show->field('wallet', __('SystemUser.wallet'));
        $show->field('integral', __('SystemUser.integral'));
        $show->field('openid', __('SystemUser.openid'));
        $show->field('avatar_url', __('SystemUser.avatar_url'));
        $show->field('province', __('SystemUser.province'));
        $show->field('city', __('SystemUser.city'));
        $show->field('gender', __('SystemUser.gender'));
        $show->field('expire_at', __('SystemUser.expire_at'));
        $show->field('last_login', __('SystemUser.last_login'));
        $show->field('longitude', __('SystemUser.longitude'));
        $show->field('latitude', __('SystemUser.latitude'));
        $show->field('level', __('SystemUser.level'));
        $show->field('distribution_level', __('SystemUser.distribution_level'));
        $show->field('remember_token', __('SystemUser.remember_token'));
        $show->field('created_at', __('SystemUser.created_at'));
        $show->field('updated_at', __('SystemUser.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SystemUser());

        $form->text('nickname', __('SystemUser.nickname'))->required();;
//        $form->text('username', __('SystemUser.username'));
        $form->mobile('phone', __('SystemUser.phone'));
        $form->email('email', __('SystemUser.email'));
        if ($form->isCreating()) {
            $form->text('password', __('SystemUser.password'))->required()
                ->help('默认：123456')
                ->default(123456);
        } else {
            $form->password('password', __('SystemUser.password'))->required()
                ->help('默认：123456');
        }

//        $form->number('parent_id', __('SystemUser.parent_id'))->default(0);
        $form->radio('gender', __('SystemUser.gender'))->options([1=>'男',2=>'女']);
//
//        $form->datetime('expire_at', __('SystemUser.expire_at'))->default(date('Y-m-d H:i:s'));
//        $form->datetime('last_login', __('SystemUser.last_login'))->default(date('Y-m-d H:i:s'));
//        $form->text('longitude', __('SystemUser.longitude'));
//        $form->text('latitude', __('SystemUser.latitude'));
//        $form->number('level', __('SystemUser.level'))->default(1);
//        $form->number('distribution_level', __('SystemUser.distribution_level'))->default(1);
        $form->saving(function (Form $form) {
//            dd($form->model()->password);
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            } else {
                $form->password = $form->model()->password;
            }
            return $form;
        });
        return $form;


    }
}
